package com.boot_reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boot_reggie.Service.AliPayService;
import com.boot_reggie.Service.impl.OrdersServiceImpl;
import com.boot_reggie.domain.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import com.alipay.easysdk.factory.Factory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/payment")
public class PaymentController {
    @Autowired
    AliPayService alipayService;
    @Autowired
    OrdersServiceImpl ordersService;
    @RequestMapping("/pay")
    public void payMent(HttpServletResponse response, HttpServletRequest request,Long id,int pirce) {
        try {
            alipayService.aliPay(response, request,id,pirce);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*得实行内网穿透*/
    @PostMapping("/notify")
    public  String payNotify(HttpServletRequest request)throws  Exception{
        String tradeNo = null;
        if(request.getParameter("trade_status").equals("TRADE_SUCCESS")){
            System.out.println("支付宝回调");
            /*支付成功*/
            Map<String, String> params = new HashMap<>();
            Map<String, String[]> requestParams = request.getParameterMap();
            for (String name : requestParams.keySet()) {
                params.put(name, request.getParameter(name));
                // System.out.println(name + " = " + request.getParameter(name));
            }

            tradeNo = params.get("out_trade_no");
            String gmtPayment = params.get("gmt_payment");
            String alipayTradeNo = params.get("trade_no");
            System.out.println("交易名称: " + params.get("subject"));
            System.out.println("交易状态: " + params.get("trade_status"));
            System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
            System.out.println("商户订单号: " + params.get("out_trade_no"));
            System.out.println("交易金额: " + params.get("total_amount"));
            System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
            System.out.println("买家付款时间: " + params.get("gmt_payment"));
            System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));







//            // 支付宝验签
//            if (Factory.Payment.Common().verifyNotify(params)) {
//                // 验签通过
//                System.out.println("交易名称: " + params.get("subject"));
//                System.out.println("交易状态: " + params.get("trade_status"));
//                System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
//                System.out.println("商户订单号: " + params.get("out_trade_no"));
//                System.out.println("交易金额: " + params.get("total_amount"));
//                System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
//                System.out.println("买家付款时间: " + params.get("gmt_payment"));
//                System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));
//
//                // 更新订单未已支付
////                ordersMapper.updateState(tradeNo, "已支付", gmtPayment, alipayTradeNo);
//            }

        }
        System.out.println("订单号是===================="+tradeNo);
        ordersService.updateOrders(tradeNo);

        return "success";
    }
}
